home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Source Code
/
C++
/
Applications
/
PICSee Dust 1.01
/
Quaternary Source
/
HandleAppleEvents.cpp
< prev
next >
Wrap
Text File
|
1995-11-01
|
8KB
|
245 lines
/* CODE EXAMPLE #1 */
// Getting Files Selected from the Finder
// A code example demonstrating how to get the files
// that the user selected in the Finder before opening
// your application. You must have the High-Level
// Event Aware flag in the application's 'SIZE' resource
// set in order for the Apple Event code to work.
// NOTE: This code is taken (pretty much verbatim)
// from Think Reference.
/*
8/15/94: Added handler for the quit event...
10/31/95: Made file independent and added some static globals, and
updated for compatibility with PowerPC compilation.
11/1/95: Added MultipleFileHandler capability.
*/
#include <GestaltEqu.h>
#include <AppleEvents.h>
#include "HandleAppleEvents.h"
typedef struct {
OpenFileHandler fileHandler;
PrintFileHandler printHandler;
QuitAppHandler quitHandler;
} AppleEventStuff;
static AppleEventStuff sPrivAppleEvents;
// ---------------------------------------------------------------------------
OSErr MyGotRequiredParams (const AppleEvent *theAppleEvent);
pascal OSErr AppHandleODoc (const AppleEvent *theAppleEvent, AppleEvent* reply, long
handlerRefCon);
pascal OSErr AppHandlePDoc (const AppleEvent *theAppleEvent, AppleEvent *reply, long
handlerRefCon);
pascal OSErr AppHandleOApp (const AppleEvent *theAppleEvent, AppleEvent *reply, long
handlerRefCon);
pascal OSErr AppHandleQApp (const AppleEvent *theAppleEvent, AppleEvent *reply, long
handlerRefCon);
// ---------------------------------------------------------------------------
Boolean AppleEventsInstalled () {
OSErr err;
long result;
// THINK C's MacTraps library provides glue for Gestalt, so
// it can be called safely under System 6. If an error is
// returned, then Gestalt for the AppleEvents Selector is
// not available (this also means that Apple Events are
// not available)
err = Gestalt (gestaltAppleEventsAttr, &result);
return (!err && ((result >> gestaltAppleEventsPresent) & 0x0001));
// return TRUE if there is no
// error and the proper bit of
// result is set
} // END AppEventsInstalled
// ---------------------------------------------------------------------------
pascal OSErr AppHandleODoc (const AppleEvent *theAppleEvent, AppleEvent* reply, long
handlerRefCon) {
FSSpec myFSS;
AEDescList docList;
OSErr err;
long index, itemsInList;
Size actualSize;
AEKeyword keywd;
DescType returnedType;
// get the direct parameter--a descriptor list--and put it into a docList
err = AEGetParamDesc (theAppleEvent, keyDirectObject,
typeAEList, &docList);
if (err)
return err;
// check for missing parameters
err = MyGotRequiredParams (theAppleEvent);
if (err)
return err;
// count the number of descriptor records in the list
err = AECountItems (&docList, &itemsInList);
// now get each descriptor record from the list, coerce the returned
// data to an FSSpec record, and open the associated file
// In our app, we can open multiple files.
// Comment out below to handle only the first file...
//if (itemsInList > 0)
// itemsInList = 1; // Handle only 1
if (sPrivAppleEvents.fileHandler != NULL && itemsInList > 0) {
FSSpec *fileList;
fileList = (FSSpec*)NewPtr(sizeof(FSSpec) * itemsInList);
if (fileList != NULL) {
for (index = 1; index <= itemsInList; index++) {
err = AEGetNthPtr (&docList, index, typeFSS, &keywd,
&returnedType, (Ptr) &myFSS, sizeof(myFSS), &actualSize);
if (err)
return err;
BlockMove(&myFSS, &fileList[index-1], sizeof(FSSpec));
}
(sPrivAppleEvents.fileHandler)(itemsInList, fileList);
DisposePtr((Ptr)fileList);
}
}
/*
else
for (index = 1; index <= itemsInList; index++) {
err = AEGetNthPtr (&docList, index, typeFSS, &keywd,
&returnedType, (Ptr) &myFSS, sizeof(myFSS), &actualSize);
if (err)
return err;
if (sPrivAppleEvents.fileHandler != NULL)
(*sPrivAppleEvents.fileHandler)(&myFSS);
}
*/
err = AEDisposeDesc (&docList);
return noErr;
} // END AppHandleODoc
// ---------------------------------------------------------------------------
pascal OSErr AppHandlePDoc (const AppleEvent *theAppleEvent, AppleEvent *reply, long
handlerRefCon) {
FSSpec myFSS;
AEDescList docList;
OSErr err;
long index, itemsInList;
Size actualSize;
AEKeyword keywd;
DescType returnedType;
// get the direct parameter--a descriptor list--and put it into a docList
err = AEGetParamDesc (theAppleEvent, keyDirectObject, typeAEList,
&docList);
if (err)
return err;
// check for missing parameters
err = MyGotRequiredParams (theAppleEvent);
if (err)
return err;
// count the number of descriptor records in the list
err = AECountItems (&docList, &itemsInList);
// now get each descriptor record from the list, coerce the returned
// data to an FSSpec record, and open the associated file
for (index = 1; index <= itemsInList; index++) {
err = AEGetNthPtr (&docList, index, typeFSS, &keywd,
&returnedType, (Ptr) &myFSS, sizeof(myFSS), &actualSize);
if (err)
return err;
if (sPrivAppleEvents.printHandler != NULL)
(*sPrivAppleEvents.printHandler)(&myFSS);
}
err = AEDisposeDesc (&docList);
return noErr;
} // AppHandlePDoc
// ---------------------------------------------------------------------------
pascal OSErr AppHandleOApp (const AppleEvent *theAppleEvent, AppleEvent *reply, long
handlerRefCon) {
// Do nothing in this case
return(noErr);
} // END AppHandleOApp
// ---------------------------------------------------------------------------
// Quit event. DO NOT CALL EXITTOSHELL. The Finder will die an unmerciful death!
pascal OSErr AppHandleQApp (const AppleEvent *theAppleEvent, AppleEvent *reply, long
handlerRefCon) {
if (sPrivAppleEvents.quitHandler != NULL)
(*sPrivAppleEvents.quitHandler)();
return(noErr);
} // END AppHandleQApp
// ---------------------------------------------------------------------------
OSErr MyGotRequiredParams (const AppleEvent *theAppleEvent) {
DescType returnedType;
Size actualSize;
OSErr err;
err = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr,
typeWildCard, &returnedType, nil, 0, &actualSize);
if (err == errAEDescNotFound) // you got all the required parameters
return noErr;
else if (!err) // you missed a required parameter
return errAEEventNotHandled;
else // the call to AEGetAttributePtr failed
return err;
} // END MyGotRequiredParams
// ---------------------------------------------------------------------------
void InitAppleEvents(
OpenFileHandler fileHandler,
PrintFileHandler printHandler,
QuitAppHandler quitHandler) {
if (AppleEventsInstalled()) {
OSErr err;
AEEventHandlerUPP aeFileHandler;
AEEventHandlerUPP aePrintHandler;
AEEventHandlerUPP aeOpenHandler;
AEEventHandlerUPP aeQuitHandler;
aeFileHandler = NewAEEventHandlerProc(AppHandleODoc);
aePrintHandler = NewAEEventHandlerProc(AppHandlePDoc);
aeOpenHandler = NewAEEventHandlerProc(AppHandleOApp);
aeQuitHandler = NewAEEventHandlerProc(AppHandleQApp);
sPrivAppleEvents.fileHandler = fileHandler;
sPrivAppleEvents.printHandler = printHandler;
sPrivAppleEvents.quitHandler = quitHandler;
err = AEInstallEventHandler (kCoreEventClass, kAEOpenDocuments,
aeFileHandler, 0, false);
err = AEInstallEventHandler (kCoreEventClass, kAEOpenApplication,
aeOpenHandler, 0, false);
err = AEInstallEventHandler (kCoreEventClass, kAEPrintDocuments,
aePrintHandler, 0, false);
err = AEInstallEventHandler (kCoreEventClass, kAEQuitApplication,
aeQuitHandler, 0, false);
}
} // END InitAppleEvents
// ==========================================================================
// ==========================================================================
// END HandleAppleEvents.c++